package com.example.download.utils

import android.content.Context
import android.os.Bundle
import android.view.*
import android.widget.TextView
import com.example.download.R


/**
 * @author: Mr.You
 * @create: 2022-11-17 10:20
 * @description:
 **/
internal class PermissionTopTipDialog() : BaseDialog() {
    var permissionType: PermissionType? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val inflate = inflater.inflate(R.layout.base_dialog_top_permission, null)
        val desc = when (permissionType) {
            PermissionType.WRITE_AND_READ -> getString(R.string.permission_write_desc)
            PermissionType.READ_PHONE_STATE -> getString(R.string.permission_device_info_desc)
            PermissionType.GPS -> getString(R.string.permission_location_desc)
            PermissionType.CAMERA -> getString(R.string.permission_camera_desc)
            PermissionType.PHONE_CALL -> getString(R.string.permission_phone_call_desc)
            else -> ""
        }
        inflate.findViewById<TextView>(R.id.tv_permission_title).text = desc
        inflate.findViewById<TextView>(R.id.tv_permission_title).setOnClickListener {
            dismiss()
        }
        return inflate
    }

    override fun onStart() {
        super.onStart()
        val window = dialog?.window ?: return
        val layoutParams = window.attributes
        layoutParams.width = getScreenWidth(window.context).minus(80)
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT
        layoutParams.gravity = Gravity.TOP
        window.attributes = layoutParams
    }

    override fun getTheme(): Int {
        return R.style.BottomDialog
    }

    private fun getScreenWidth(context: Context): Int {
        val manager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = manager.defaultDisplay
        return display.width
    }

}